__all__ = ['MTZone']

import os
import pymt
from OpenGL.GL import *
from pymt import gx_matrix, drawRectangle, set_color, gx_stencil, stencilUse
from pymt import drawRoundedRectangle, drawTexturedRectangle
from pymt import matrix_inv_mult
from pymt import SafeList
from pymt import MTRectangularWidget
from pymt import MTScatterWidget
from pymt import MTImageButton, MTButton
from pymt import MTWidget

iconPath = os.path.join(os.path.dirname(pymt.__file__), 'data', 'icons', '')

class MTZoneContainer(MTRectangularWidget):
    def __init__(self, **kwargs):
        kwargs.setdefault('cls', 'zoneContainer')
        super(MTZoneContainer, self).__init__(**kwargs)

    def add_on_key_press(self, *largs, **kwargs):
        return self.parent.parent.get_parent_window().add_on_key_press(*largs, **kwargs)
    def remove_on_key_press(self, *largs, **kwargs):
        return self.parent.parent.get_parent_window().remove_on_key_press(*largs, **kwargs)
    def get_on_key_press(self, *largs, **kwargs):
        return self.parent.parent.get_parent_window().get_on_key_press(*largs, **kwargs)

    def add_on_text(self, *largs, **kwargs):
        return self.parent.parent.get_parent_window().add_on_text(*largs, **kwargs)
    def remove_on_text(self, *largs, **kwargs):
        return self.parent.parent.get_parent_window().remove_on_text(*largs, **kwargs)
    def get_on_text(self, *largs, **kwargs):
        return self.parent.parent.get_parent_window().get_on_text(*largs, **kwargs)

    def add_on_text_motion(self, *largs, **kwargs):
        return self.parent.parent.get_parent_window().add_on_text_motion(*largs, **kwargs)
    def remove_on_text_motion(self, *largs, **kwargs):
        return self.parent.parent.get_parent_window().remove_on_text_motion(*largs, **kwargs)
    def get_on_text_motion(self, *largs, **kwargs):
        return self.parent.parent.get_parent_window().get_on_text_motion(*largs, **kwargs)

    def add_on_text_motion_select(self, *largs, **kwargs):
        return self.parent.parent.get_parent_window().add_on_text_motion_select(*largs, **kwargs)
    def remove_on_text_motion_select(self, *largs, **kwargs):
        return self.parent.parent.get_parent_window().remove_on_text_motion_select(*largs, **kwargs)
    def get_on_text_motion_select(self, *largs, **kwargs):
        return self.parent.parent.get_parent_window().get_on_text_motion_select(*largs, **kwargs)


class MTZone(MTScatterWidget):

    def __init__(self, parent_height, parent_width,**kwargs):
        kwargs.setdefault('control_scale', 1.0)			
        super(MTZone, self).__init__(**kwargs)
        self.content=None
        self.control_scale = kwargs.get('control_scale')
        self.height_max = parent_height
        self.height_min = 50
        self.width_max = parent_width
        self.width_min = 160
        if(self.height<self.height_min):
			self.height=self.height_min
        if(self.height>self.height_max):
			self.height=self.height_max
        if(self.width<self.width_min):
			self.width=self.width_min
        if(self.width>self.width_max):
			self.width=self.width_max
        self.container = MTZoneContainer(pos=(0,0), size=self.size)
        super(MTZone, self).add_widget(self.container)			
			
    def close(self, touch):
        self.parent.remove_widget(self)

    def add_widget(self, w):
        s = str(type(w))
        if not (s.find('MTWidget')>= 0 or s.find('MTButtonsZone')>= 0):
			self.content=w		
        self.container.add_widget(w)

    def remove_widget(self, w):
        s = str(type(w))
        if not (s.find('MTWidget')>= 0 or s.find('MTButtonsZone')>= 0):
			self.content=None	
        self.container.remove_widget(w)

    def delete(self, touch):
        self.parent.remove_widget(self)		

    def get_parent_window(self):
        return self.container

    def get_scaled_border(self):
        return self.style.get('border-width') * (1.0 / self.scale)

    def on_touch_down(self, touch):
        touch.push()
        touch.x, touch.y = super(MTZone, self).to_local(touch.x, touch.y)
        touch.pop()

        if super(MTZone, self).on_touch_down(touch):
            return True

    def on_touch_move(self, touch):
        if touch.grab_current == self:
            touch.push()
            touch.x, touch.y = super(MTZone, self).to_local(touch.x, touch.y)
            touch.pop()
        return super(MTZone, self).on_touch_move(touch)

    def on_touch_up(self, touch):
        if touch.grab_current == self:
            touch.push()
            touch.x, touch.y = super(MTZone, self).to_local(touch.x, touch.y)
            touch.pop()
        return super(MTZone, self).on_touch_up(touch)

    def collide_point(self, x, y):
        scaled_border = self.get_scaled_border()
        local_coords = super(MTZone,self).to_local(x, y)
        left, right = -scaled_border, self.width + scaled_border * 2
        bottom,top = -scaled_border, self.height + scaled_border * 2
        if local_coords[0] > left and local_coords[0] < right \
           and local_coords[1] > bottom and local_coords[1] < top:
            return True
        else:
            return False

    def draw(self):
        # select color from number of touch
        if len(self.touches) == 0:
            set_color(*self.style.get('bg-color'))
        elif len(self.touches) == 1:
            set_color(*self.style.get('bg-color-move'))
        else:
            set_color(*self.style.get('bg-color-full'))

        # draw border
        scaled_border = self.get_scaled_border()
#        self.update_controls()
        drawRectangle(
            pos=(-scaled_border, -scaled_border),
            size=(self.width+scaled_border*2, self.height+scaled_border*2),
)

        # gradient
        set_color(1, 1, 1, 0.005)
        for step in range(5, 11):
          scaled_border -= (scaled_border * (step / 10.0))
          drawRectangle(
              pos=(-scaled_border, -scaled_border),
              size=(self.width+scaled_border*2, self.height+scaled_border*2))

    def on_draw(self):
        with gx_matrix:
            glMultMatrixf(self.transform_mat)

            self.draw()
            # use stencil for container
            with gx_stencil:
                drawRectangle((0, 0), size=self.size)
                stencilUse()
                self.container.dispatch_event('on_draw')

    def on_move(self, x, y):
        # no move on children
        pass

    def on_resize(self, w, h):
        # no resize of children
        pass

    def _get_content(self):
        return self.content
    id = property(_get_content)
	
    def _get_top(self):
        top=self.center[1]+(self.height/2)
        return top
    id = property(_get_top)
	
    def _get_left(self):
        left=self.center[0]-(self.width/2)
        return left
    id = property(_get_left)
	
    def _get_width(self):
        return self.width
    id = property(_get_width)

    def _get_height(self):
        return self.height
    id = property(_get_height)	